/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import basedatos.OperacionesBD;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import modulos.usuario.modelo.Permiso;
import modulos.usuario.modelo.Usuario;
import vista.modelo.CopiaArchivos;
import vista.modelo.OperacionesVarias;

/**
 *
 * @author victor
 */
public class iFrmSonidos extends javax.swing.JInternalFrame {

    /**
     * Creates new form iFrmSonidos
     */
    public iFrmSonidos() {
        initComponents();
        OperacionesVarias.cerrarVentanaEscape(this);
        filtroSonido = new FileNameExtensionFilter("Archivos de sonido", "mp3", "wav");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        pnlId2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setTitle("Configuraciones Alarmas");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/bell.png"))); // NOI18N

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        jButton1.setText("Cerrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        pnlId2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP, null, null));

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N
        jLabel1.setText("Seleccione el Tono de la Alarma:");

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/bell_add.png"))); // NOI18N
        jButton2.setToolTipText("Clic para elegir el tono");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlId2Layout = new javax.swing.GroupLayout(pnlId2);
        pnlId2.setLayout(pnlId2Layout);
        pnlId2Layout.setHorizontalGroup(
            pnlId2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlId2Layout.createSequentialGroup()
                .addGroup(pnlId2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlId2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(pnlId2Layout.createSequentialGroup()
                        .addGap(124, 124, 124)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(68, Short.MAX_VALUE))
        );
        pnlId2Layout.setVerticalGroup(
            pnlId2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlId2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/accept.png"))); // NOI18N
        jButton3.setText("Aceptar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnlId2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(64, 64, 64))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlId2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton3))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        JFileChooser fc = new JFileChooser();
        fc.setFileFilter(filtroSonido);
        int opc = fc.showOpenDialog(this);
        if (opc == JFileChooser.APPROVE_OPTION) {
            try {
                archivo = fc.getSelectedFile();
                dirdestino = System.getProperty("user.dir") + "/alarmas/" + archivo.getName();
            } catch (Exception ex) {
                Logger.getLogger(iFrmSonidos.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (opc == JFileChooser.CANCEL_OPTION) {
            System.out.println("Se ha cancelado");
        }

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        try {
            File destino = new File(dirdestino);
            CopiaArchivos.Copiar(archivo, destino);
            if (modificaAlarma(dirdestino)) {
                JOptionPane.showMessageDialog(null, "Se ha cambiado el tono de alarma correctamente");
            } else {
                JOptionPane.showMessageDialog(null, "No se ha podido cambiar el tono de alarma");
            }
        } catch (Exception ex) {
            Logger.getLogger(iFrmSonidos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton3ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel pnlId2;
    // End of variables declaration//GEN-END:variables
    FileNameExtensionFilter filtroSonido;
    File archivo;
    String dirdestino;

    private boolean modificaAlarma(String dirAlarma) {
        Usuario usuario = (Usuario) frmPrincipal.UsuarioLogueado.get(0)[1];
        Permiso permiso = (Permiso) OperacionesBD.buscar("Permiso", "usuario_idPersona", usuario.getIdPersona() + "");
        permiso.setDirAlarma(dirAlarma);
        if (OperacionesBD.modificar(permiso)) {
            return true;
        } else {
            return false;
        }
    }
}
